local Skill = import(".Skill")
local Talent = class("Talent")

local string_find = string.find
local string_gsub = string.gsub
local string_match = string.match

function Talent:ctor()
    self.id = 0

    self.isNew = false
	self.active = false
end

function Talent:isValid()
    return self._skillConf ~= nil
end

function Talent:read(value)
    self.id = tonumber(value.uid)

    -- talent 实际是一个技能，有一些属性在talent表
    self._skillConf = GD:getClassSkill(self.id)
    self._conf = GD:getTalentConfByTeam(tonumber(self._skillConf.class), tonumber(self._skillConf.team))

    self.specId = tonumber(self._conf.followSkill)
    self.seat = tonumber(self._conf.seat)

    -- 前置条件
    self.preSkillId = tonumber(self._skillConf.preSkill or 0)
    self.preSeat = 0
    if self.preSkillId > 0 then
        local preSkillConf = GD:getClassSkill(self.preSkillId)
        local preConf = GD:getTalentConfByTeam(tonumber(preSkillConf.class), tonumber(preSkillConf.team))
        self.preSeat = tonumber(preConf.seat)
    end

    self._skill = nil
    self._icon = nil
end

function Talent:getSkill()
    if not self._skill then
        self._skill = Skill.new()
        self._skill:read({uid=self.id})
    end
    return self._skill
end
-- 返回天赋提供的属性列表
function Talent:getSkillAttr( specId )
    assert( specId, " ------------------>>>>>> Talent:getSkillAttr 必须指定专精ID" )

    local attrList = {}
    if self:getSpec() == tonumber( specId ) then
        local skill = self:getSkill()
        for k,v in pairs( skill:getAttrs() ) do
            UD:MergeAttr( attrList, { k, v } )
        end
    end
    return attrList
end

function Talent:getId()
    return self.id
end

function Talent:getName()
    return L(self._skillConf.name)
end

function Talent:getIcon()
    if not self._icon then
        self._icon = Res.skillIconPath .. self._skillConf.icon .. ".png"
    end
    return self._icon
end

function Talent:getCollection()
    local collection=""
    if self._skillConf and self._skillConf.collection then
        collection=L(self._skillConf.collection)
    end
    return collection
end

function Talent:getTeam()
    return tonumber(self._skillConf.team)
end

function Talent:getSeat()
    return self.seat
end

function Talent:getPreSeat()
    return self.preSeat
end

-- 前置条件是否激活
function Talent:isPreActive()
    if self.preSkillId > 0 then
        return UD:hasTalent(self.preSkillId)
    end
    return true
end

function Talent:getSpec()
    return self.specId
end

function Talent:getMinLv()
    return tonumber(self._skillConf.minLv or 1)
end

function Talent:isFirst()
    return self.preSkillId == 0
end

function Talent:getPreSkillId()
    return self.preSkillId
end

-- 是否可以被激活 1.未激活 2.等级满足 3.前置条件激活
function Talent:canActive()
    return not self.active and self:isPreActive() and self:getMinLv() <= UD:getLevel()
end

function Talent:setIsNewGet(isNew)
	self.isNew = isNew
end

function Talent:setActive(a)
	self.active = a
end

function Talent:isActive()
	return self.active
end

-- 只需解析一次就够了，又不会改变
function Talent:getDesc()
    if not self._desc then
        local hasAtcritrt = false -- 是否暴击属性？
        local hasAthitrt = false -- 是否命中属性？
        local hasAtdodgert = false -- 是否闪避属性？
        local curSkill = self:getSkill()
        for k,v in pairs( curSkill:getAttrs() ) do
            if 35 == tonumber( k ) then -- 暴击属性
                hasAtcritrt = true
            elseif 40 == tonumber( k ) then -- 命中属性
                hasAthitrt = true
            elseif 45 == tonumber( k ) then -- 闪避属性
                hasAtdodgert = true
            end
        end

        local desc = L(self._skillConf.des or "")
        if hasAtcritrt or hasAthitrt or hasAtdodgert then
            local patternStr = "%%s"
            if string_find( desc, patternStr ) then
                local tempAttr_1 = 0
                local tempAttr_2 = 0

                if hasAtcritrt then -- 暴击？
                    tempAttr_1 = math.floor( UD:getTalentAtcritrt() )
                end
                if hasAthitrt then -- 命中？
                    tempAttr_2 = math.floor( UD:getTalentAthitrt() )
                end
                if hasAtdodgert then -- 闪避？
                    tempAttr_1 = math.floor( UD:getTalentAtdodgert() )
                end

                local patternStr2 = "#(.*)#"
                if 0 >= tempAttr_1 then -- 未激活的暴击值取不到？
                    desc = string_gsub( desc, patternStr2, L("lua_code_text_48") )
                else
                    local str = string_match( desc, patternStr2 )
                    local strTemp = string.format( str, tempAttr_1 )
                    desc = string_gsub( desc, patternStr2, strTemp )
                    desc = string_gsub( desc, "#", "" )
                end

                patternStr2 = "*(.*)*"
                if 0 >= tempAttr_2 then -- 未激活的命中值取不到？
                    desc = string_gsub( desc, patternStr2, L("lua_code_text_48") )
                else
                    local str = string_match( desc, patternStr2 )
                    local strTemp = string.format( str, tempAttr_2 )
                    desc = string_gsub( desc, patternStr2, strTemp )
                    desc = string_gsub( desc, "*", "" )
                end
            end
        end

        self._desc = desc
    end
    return self._desc
end

return Talent
